const express = require('express');
const path = require('path');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const wechatRouter = require('./routes/wechat');

const app = express();
const port = 3000;

// 设置静态文件目录
app.use(express.static(path.join(__dirname, 'public')));

// 设置视图目录和引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 使用express.json()中间件来解析JSON请求体
app.use(express.json());

// 使用路由
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/wechat', wechatRouter);

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).render('error');
});

// 启动服务器
app.listen(port, () => {
  console.log(`服务器正在运行在 http://localhost:${port}`);
});